﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CP.Entities;

namespace CP.DataAccess
{
    public class CategoriaDAO: MasterDAL, ICategoriaDAO
    {
        public List<Categoria>  GetAllFromCategoria()
        {
 	        SqlCommand command = base.CreateCommand("GetAllFromCategoria");

            return SelectObjectFactory(command);
        }

        public List<Categoria>  GetFromCategoriaByCriterio(string criterio)
        {
            SqlCommand command = base.CreateCommand("GetFromCategoriaByDescripcion");

            base.AddInParameter(command, "descripcion", criterio);

            return SelectObjectFactory(command);
        }

        public Categoria  GetFromCategoriaById(int id)
        {
            SqlCommand command = base.CreateCommand("GetFromCategoriaById");

            base.AddInParameter(command, "id", id);

            return SelectObjectFactory(command).SingleOrDefault();
        }

        public void  RegistrarCategoria(Categoria categoria)
        {
 	        throw new NotImplementedException();
        }

        public void  EliminarCategoria(int id)
        {
 	        throw new NotImplementedException();
        }

        private List<Categoria> SelectObjectFactory(SqlCommand command)
        {
            List<Categoria> lista = new List<Categoria>();

            using (SqlDataReader reader = base.ExecuteReader(command))
            {
                while (reader.Read())
                {
                    Categoria categoria =
                        new Categoria()
                        {
                            Id = reader.GetInt32(0),
                            Descripcion = reader.GetString(1),
                            Comentario = reader.GetString(2)
                        };

                    lista.Add(categoria);
                }
            }

            return lista;
        }
    }
}
